<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/4/11
 * Time: 15:14
 * Mail:87658989@qq.com
 */

namespace app\api\controller\offical;

use think\Controller;

class Base extends Controller
{
    public $page;
    public $offset;
    public $devkey;
    public function _initialize()
    {
        $this->page   = input('page',1);
        $this->offset = input('offset',20);
        $this->devkey = input('devkey',null);
    }

    public function retSucc($d=[]){
        $data['status'] = 1;
        $data['info']   = 'ok';
        $data = array_merge($data,$d);
        return json($data);
    }

    public function retErr($msg='error'){
        $data['status'] = 0;
        $data['info']   = $msg;
        return json($data);
    }

    /**
     * 变量批量获取 及 校验变量
     * @param string $string
     * @param string $method
     * @return array
     */
    public function getBatchParam($string='',$method=''){
        // order_id|1|oid#订单id@require|email/order_no#单号@require|email
        $data = [];
        $arr  = explode('/',$string);
        $rule = [];
        foreach ($arr as $v){
            $ruleArr        = explode('#',$v);
            $arr1           = explode('|',$ruleArr[0]);
            $name           = ($arr1[2] && $arr1[2]<>'-') ? $arr1[2] : $arr1[0];
            if(isset($ruleArr[1])){
                $holeName = explode('@',$ruleArr[1]);
                if(isset($holeName[1])){
                    $rule[$name.'|'.$holeName[0]] = $holeName[1];
                }else{
                    $rule[$name] = $holeName[0];
                }
            }
            $method         = ($arr1[3] && $arr1[3]<>'-') ? $arr1[3].'.' : $method.'.';
            $vari           = ($arr1[3] && $arr1[3]<>'-') ? $arr1[3].'.'.$arr1[0] : $arr1[0];
            $data[$name]    = isset($arr1[1]) ? input($vari,$arr1[1]) : input($vari);
        }
        if(!empty($rule)){
            $validate = new Validate($rule);
            if (!$validate->check($data)) {
                abort($this->requestStatus,$this->getErrorMsg($validate->getError()));
            }
        }
        return $data;
    }

    public function batchParam($string,$method=''){
        return new \app\api\logic\Request($this->getBatchParam($string,$method));
    }
}